package com.zxy.shaozhiyi1.main;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PushbackInputStream;
import java.util.List;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By.ById;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class TestTestNG1 {
	public WebDriver driver = null;
	public WebElement webelement = null;
	
	@Test(invocationCount = 1)
	public void loadTestThisWebsite1() throws InterruptedException, IOException
	{
		//讲谷歌驱动拷贝到项目下 "D:\\geckodriver.exe");
//      System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		
		
		// Firefox浏览器
		// System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
		// System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe");
		// System.setProperty("webdriver.firefox.marionette", "D:\\geckodriver.exe");
		// WebDriver driver = new FirefoxDriver();
		System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("https://isisn.nsfc.gov.cn/egrantindex/funcindex/prjsearch-list");
		System.out.println("Page Title is " + driver.getTitle());
		// AssertJUnit.assertEquals("Google", driver.getTitle());
		
		// 在20s之内操作树，年份选择，图片验证码输入
		// 树展开按钮
		webelement = driver.findElement(By.xpath("//a[@id = \"f_subjectCode_img\"]"));
		// 点击
		webelement.click();
		
		// 树展开
		
		Thread.sleep(5000);// 5s
		
		// 树DIV
		webelement = driver.findElement(By.xpath("//div[@id = \"f_subjectCode_treeId\"]"));
		// 所有一级节点
		List<WebElement> webelements1 = webelement.findElements(By.tagName("li"));
		System.out.println("1111111111-----   " + webelements1.size());
		
		// 所有一级节点的第一个节点（A.数理科学部）
		// 左边 +号 按钮
		webelement = webelements1.get(0).findElement(By.tagName("button"));
		// 点击 +号 按钮
		webelement.click();
		for (WebElement webElement : webelements1) {
			
		}
		
		// 展开（A.数理科学部）
		
		Thread.sleep(5000);// 5s
		
		webelement = webelements1.get(0).findElement(By.tagName("ul"));
		// 所有2级节点
		List<WebElement> webelements2 = webelement.findElements(By.tagName("li"));
		// +号 按钮  后面的 链接
		webelement = webelements2.get(0).findElement(By.tagName("a"));
		// 点击  +号 按钮  后面的 链接，把值给textbox
		webelement.click();
		
		// 展开（A.数理科学部）
		
		Thread.sleep(5000);// 5s
		
		/*// +号 按钮  后面的 链接
		webelement = webelements1.get(0).findElement(By.tagName("a"));
		// 点击  +号 按钮  后面的 链接，把值给textbox
		webelement.click();*/
		
		Thread.sleep(5000);// 5s
		
		/*webelement = driver.findElement(By.xpath("//div[@id = \"f_subjectCode_treeId\"]"));
		
		
		
		
		// A.数理科学部 前面的+号
		webelement = driver.findElement(By.xpath("//button[@id = \"f_subjectCode_treeId_115_switch\"]"));
		// 点击
		webelement.click();
		
		Thread.sleep(5000);// 5s
		
		// A01.数学
		webelement = driver.findElement(By.xpath("//span[@id = \"f_subjectCode_treeId_87_span\"]"));
		// 点击
		webelement.click();*/
		
		Thread.sleep(5000);// 5s
		
//		Select select = (Select) driver.findElement(By.xpath("//select[@id = \"f_year\"]"));
		Select sel = new Select(driver.findElement(ById.xpath("//select[@id='f_year']")));
		sel.selectByValue("2016");
		
		Thread.sleep(10000);// 20s
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		String filePath1 = "d:" + File.separator + "test111111111111.xls";
//		String filePath2 = "D:\\test333333333333.xls";
		String filePath2 = "D:" + File.separator + "test333333333333.xls";
		
		// 第1步、使用File类找到一个文件
		// 声明File对象
		File file = new File(filePath1);
		// 第2步、通过子类实例化父类对象
		OutputStream outputStream = null; // 准备好一个输出的对象
		// out = new FileOutputStream(file) ; // 通过对象多态性，进行实例化
		outputStream = new FileOutputStream(file, true); // 通过对象多态性，进行实例化
		
		/*
		 * // 第3步、进行写操作
		 * String str = "Hello World!!!" ; // 准备一个字符串 byte b[] =
		 * str.getBytes() ; // 只能输出byte数组，所以将字符串变为byte数组 out.write(b) ; // 将内容输出，保存文件 //
		 * 第4步、关闭输出流 out.close() ; // 关闭输出流 //文件不存在会自动创建
		 */

		// BufferedWriter bufout = new BufferedWriter(new OutputStreamWriter(new
		// FileOutputStream("d:" + File.separator + "test111111.xls", true)));
		
//		FileOutputStream fileOutputStream = new FileOutputStream(filePath2);

//		ExcelUtil.exportExcel(filePath2, "titile", headMap, jsonArray, null, 20, fileOutputStream, rowIndex);
		
		FileInputStream fileInputStream = new FileInputStream(filePath2);
		Workbook workbook = getWorkbook(fileInputStream);
		
	}
	
	public static Workbook getWorkbook(InputStream is) throws IOException {
		Workbook wb = null;
		if (!is.markSupported()) {
			is = new PushbackInputStream(is, 8);
		}
		if (POIFSFileSystem.hasPOIFSHeader(is)) { // Excel2003及以下版本
			wb = (Workbook) new HSSFWorkbook(is);
		} else if (POIXMLDocument.hasOOXMLHeader(is)) { // Excel2007及以上版本
			wb = new XSSFWorkbook(is);
		} else {
			throw new IllegalArgumentException("你的Excel版本目前poi无法解析！");
		}
		// }
		return wb;
	}
	
	
	// 封装highlight
		public WebElement highLight(WebElement webElement) {
			JavascriptExecutor js = (JavascriptExecutor) driver;
			js.executeScript("arguments[0].setAttribute('style', arguments[1]);", webElement,
					"background: yellow;color: red; border: 2px solid red;");
			return webElement;
		}

		@Test(priority = 10)
		public void searchCSDN() throws InterruptedException {
			webelement = driver.findElement(By.xpath("//input[@id = \"kw\"]"));
			highLight(webelement);
			webelement.sendKeys("csdn");

			webelement = driver.findElement(By.xpath("//*[@id=\"su\"]"));
			highLight(webelement);
			webelement.click();

			Thread.sleep(3001);
		}
		
		/**
		 * @param args
		 */
		public static void main1(String[] args) {
			System.setProperty("webdriver.firefox.marionette", "D:\\geckodriver.exe");
			System.setProperty("webdriver.firefox.bin", "D:\\developtools\\Mozilla Firefox\\firefox.exe");
			WebDriver driver = new FirefoxDriver();
			driver.get("https://isisn.nsfc.gov.cn/egrantindex/funcindex/prjsearch-list");
			System.out.println("1 Page title is: " + driver.getTitle());
			WebElement element = driver.findElement(By.id("kw"));
			// 输入关键字
			element.sendKeys("zTree");
			element.submit();
			(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
				public Boolean apply(WebDriver d) {
					return d.getTitle().toLowerCase().endsWith("ztree");
				}
			});
			System.out.println("2 Page title is: " + driver.getTitle());
			// 关闭浏览器
			driver.quit();
		}
		
		public static void method1(String file, String conent) {
			BufferedWriter out = null;
			try {
				out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
				out.write(conent);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (out != null) {
						out.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
}
